# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libarchive
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.7.1
pkgrel=1
pkgdesc="Multi-format archive and compression library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://www.libarchive.org/"
license=("BSD")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-autotools")
depends=(${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-bzip2
         ${MINGW_PACKAGE_PREFIX}-expat
         ${MINGW_PACKAGE_PREFIX}-libb2
         ${MINGW_PACKAGE_PREFIX}-libiconv
         ${MINGW_PACKAGE_PREFIX}-lz4
         ${MINGW_PACKAGE_PREFIX}-libsystre
         ${MINGW_PACKAGE_PREFIX}-openssl
         ${MINGW_PACKAGE_PREFIX}-xz
         ${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-zstd)
source=("https://github.com/libarchive/libarchive/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.xz")
sha256sums=('b17403ce670ff18d8e06fea05a9ea9accf70678c88f1b9392a2e29b51127895f')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  autoreconf -fiv
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  # Mention default options explicitly to clarify any confusion
  ../${_realname}-${pkgver}/configure \
    --build="${MINGW_CHOST}" \
    --host="${MINGW_CHOST}" \
    --target="${MINGW_CHOST}" \
    --prefix="${MINGW_PREFIX}" \
    --without-xml2 \
    --without-lzo2 \
    --without-nettle \
    --with-openssl \
    --with-cng

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  make check
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install
 
  #fix issue with hard-coded path in libarchive.pc file.
  _localpath=$(cygpath -wm ${MINGW_PREFIX})
  sed -i -e "s|${_localpath}|${MINGW_PREFIX}|g" ${pkgdir}/${MINGW_PREFIX}/lib/pkgconfig/libarchive.pc
}
